/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.core.fadingmessage;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;

import org.mopore.jat.gui.FadingMode;

public class FadingMessageWindowAssistance {

	private static final float ALPHA_MAX = .85f;
	
	private final static float[] alphaArray = new float[40];

	private int fadingIndex;
	
	private FadingMode currentFadingMode;
	
	private boolean exiting;
	
	private boolean mouseIsIn;
	
	private boolean buttonHover;
	
	private int presentCount;
	
	private static final int MAX_PRESENT = 100;

	private Area buttonArea;

	private boolean mouseDown;

	private ActionListener actionListener;

	private FadingMessageWindow messageWindow;

	private int quickFadeOut;
	
	
	public FadingMessageWindowAssistance( ActionListener actionListener, 
			FadingMessageWindow messageWindow ){
		this.actionListener = actionListener;
		this.messageWindow = messageWindow;
		
		fadingIndex = 0;
		
		float factor = ALPHA_MAX / (alphaArray.length);
	    for (int i = 0; i < alphaArray.length; i++) {
	    	float value = i * factor;
	    	alphaArray[i] = value;
		}

		mouseIsIn = false;
		exiting = false;
		buttonHover = false;
		mouseDown = false;
		currentFadingMode = FadingMode.FADING_IN;
		
		buttonArea = new Area( new Rectangle2D.Float( 224, 10, 16, 10 ) );
	}
	
	
	public void forceFadeOut(){
		exiting = true;
		currentFadingMode = FadingMode.FADING_OUT;
		quickFadeOut = 4;
	}
	
	
	public FadingMode calculate(){
		
		switch ( currentFadingMode ) {
			case FADING_IN:{
				
				boolean enoughPresent = ( presentCount >= MAX_PRESENT );
				if ( enoughPresent && !mouseIsIn ) {
					currentFadingMode = FadingMode.FADING_OUT;
				}
				else {
					fadingIndex ++;
					
					boolean maxFadeIn = ( fadingIndex > alphaArray.length -1 );
					if ( maxFadeIn ) {
						currentFadingMode = FadingMode.PRESENT;
						fadingIndex = alphaArray.length -1;
					}
					break;
				}
			}
			case PRESENT:{
				presentCount ++;
				boolean enoughPresent = ( presentCount >= MAX_PRESENT );
				if ( enoughPresent ){
					if ( ! mouseIsIn ) {
						currentFadingMode = FadingMode.FADING_OUT;
					}
				}
				break;
			}
			case FADING_OUT:{
				fadingIndex --;
				fadingIndex = fadingIndex - quickFadeOut;
				if ( fadingIndex <= 0 ) {
					fadingIndex = 0;
					currentFadingMode = FadingMode.DONE;
				}
				
				if ( mouseIsIn && ! exiting ) {
					currentFadingMode = FadingMode.FADING_IN;
				}
				break;
			}
			default:
				return FadingMode.DONE;
		}
		return currentFadingMode;
	}


	
	public float getTransparency(){
		float value = alphaArray[ fadingIndex ];
		return value;
	}
	
	
	public void mouseIn() {
		mouseIsIn = true;
	}


	public void mouseOut() {
		mouseIsIn = false;
	}




	public void mouseMoved(MouseEvent e) {
		buttonHover = false;
		if ( mouseIsIn ) {
			Point point = e.getPoint();
			boolean b = buttonArea.contains( point );
			if ( b ) {
				buttonHover = true;
			}
		}
	}


	public boolean isButtonHover() {
		return buttonHover;
	}
	
	
	public boolean isButtonDown() {
		boolean buttonDown = ( buttonHover && mouseDown );
		return buttonDown;
	}
	
	public void mouseDown() {
		this.mouseDown = true;
	}
	
	public void mouseUp() {
		
		if ( mouseIsIn ) {
			forceFadeOut();
			if ( ! buttonHover) {
				if ( actionListener != null ) {
					ActionEvent event = new ActionEvent( messageWindow, 0, 
							"message window clicked");
					actionListener.actionPerformed( event );
				}
			}
		}
		this.mouseDown = false;
	}


	public boolean isMouseIsIn() {
		return mouseIsIn;
	}
	
	
}
